export function pickDate(list) {
  let dateMap = {}
  list.reduce((dmap, item) => {
    dmap[item.date] = item.date
    return dmap
  }, dateMap)

  return Object.keys(dateMap)
}

export function CModel(pos, contract) {
  // 有可能有重复的合约
  let data = contract ? [contract] : []
  return {
    pos,
    data
  }
}

export function CCollection(dateArr, priceArr, list) {
  let CMap = listToMap(list)
  let cc = []
  priceArr.forEach((p, i) => {
    cc[i] = []
    dateArr.forEach((d, j) => {
      let pos = d + p
      // 存在合约
      if (CMap[pos]) {
        cc[i][j] = CMap[pos]
      }
      // 合约为空
      else {
        cc[i][j] = CModel(pos, null)
      }
    })
  })
  return cc
}

function listToMap(list) {
  let map = {}
  list.forEach(c => {
    let pos = c.date + c.executionPrice
    if (!map[pos]) {
      map[pos] = CModel(pos, c)
    } else {
      map[pos].data.push(c)
    }
  })
  return map
}

export default {
  pickDate,
  CCollection
}